<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

include_once __DIR__.'/config.php';

$exchange_name = 'test1_exchange';
$routing_key   = 'test1_routing_key';

//创建连接
$connection = new AMQPStreamConnection(HOST,PORT,USER,PASS,VHOST);

$channel = $connection->channel();

//测试不绑定路由器不绑定交换机，直接发送消息
$channel->exchange_declare($exchange_name,AMQP_EX_TYPE_DIRECT);

$centent = '这是一个漂亮的demo';

$message = new AMQPMessage($centent);

//消息推送，第四个参数设置为true表示输出消息推送是的erro
$channel->basic_publish($message,$exchange_name,$routing_key,true);

$wait = true;

//监听消息模板
$returnListener = function (
    $replyCode,
    $replyText,
    $exchange,
    $routingKey,
    $message
) use(&$wait){
    $wait = false;

    echo "return:".PHP_EOL,
    "replyCode  :$replyCode".PHP_EOL,
    "replyText  :$replyText".PHP_EOL,
    "exchange   :$exchange".PHP_EOL,
    "routingKey :$routingKey".PHP_EOL,
    "message    :$message->body".PHP_EOL;
};

//设置监听模式
$channel->set_return_listener($returnListener);

while ($wait) {
    //消息循环输出到控制台
    $channel->wait();
}

$channel->close();
$connection->close();
?>